﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MNNUTEM
{
    public partial class Inventario : Form
    {

        private int idProducto, sucursal, idCosto, idInventario=1, cantidadp, idInv;
        private float demanda,rop,rop2;
        private float costoA, costoB,T,F,Q,p,Qfaltante,Tfaltante,Ffaltante,S;
        private crudInventario cInventario;
        public Inventario()
        {
            InitializeComponent();
            cInventario = new crudInventario();
        }

        public void woStock()
        {

            /*DataSet DS;
            cInventario.Consultation3("SELECT  rop, cantidadProducto,if (rop< cantidadProducto, 'yes' , 'not') FROM Inventario JOIN Producto WHERE Producto_idProducto = idProducto", dataGridView1);*/
            cInventario.Consultation3(dataGridView2);
           
            

           
              



            textBox1.Text = "" + idProducto;

        }
        private void getUltimate()
        {

            DataSet DS;
            DS = cInventario.Consultation2("select Max(idProducto) from Producto");
            idProducto = int.Parse(DS.Tables[0].Rows[0][0].ToString());
            
            textBox1.Text = "" + idProducto;


        }
        private void Inventario_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = cInventario.Consultation("SELECT idInventario, QInventario, DInventario, AInventario, CInventario, FInventaio, tipoInventario, rop, idProducto, nombreProducto, cantidadProducto FROM Inventario JOIN Producto WHERE Producto_idProducto = idProducto");
            dataGridView2.DataSource = cInventario.Consultation("select *from Producto");
            button4.Enabled = false;
            button3.Enabled = false;
            getUltimate(); getvar(); woStock();

        }

        private void updateId(int idProducto)
        {

            DataSet DS;
            DS = cInventario.Consultation2("select count(idInventario) from Inventario");
            int count = int.Parse(DS.Tables[0].Rows[0][0].ToString()) + 1;

            if (idProducto != count)
                for (int i = idProducto; i <= count; i++)
                {

                    cInventario.SetId(i, i + 1);
                }
            else { }

        }

        private void updateGridView()
        {
            dataGridView1.DataSource = cInventario.Consultation("select *from Inventario");

        }

        private void dataGridView2_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            idProducto = int.Parse(dataGridView2[0, e.RowIndex].Value.ToString());
            textBox1.Text = idProducto.ToString();
            getvar();
            
        }

        private void ECQ()
        {

           Q = (float)System.Math.Sqrt((2*demanda*costoA)/costoB);
           textBox7.Text = ""+Q;
        
        
        }


        private void ECQFALTANTE()
        {

            Q = (float)System.Math.Sqrt((2 * demanda * costoA) / costoB);
            textBox7.Text = "" + Q;

            Qfaltante = (float)(System.Math.Sqrt((2 * demanda * costoA) / costoB)*(System.Math.Sqrt((p+costoB)/p)));
            S = (float)(System.Math.Sqrt((2 * demanda * costoA) / costoB)*(System.Math.Sqrt(p/(p+costoB))));
            Tfaltante = Qfaltante / demanda;
            Ffaltante = 1 / T;
            textBox13.Text = "" + Ffaltante;
            textBox14.Text = "" + Qfaltante;
            textBox16.Text = "" + Tfaltante;
            textBox18.Text = "" + S;
            textBox19.Text = "" + (Q-S);




        }

        private void getvar()
        { 
          //  Q,demanda,adquisicion, bodega,
            DataSet DS;
            DS = cInventario.Consultation2("SELECT p.Demanda, p.Sucursal_idSucursal, c.idCosto, c.transporteCosto, c.bodegaCosto, c.faltanteCosto, c.adquisicionCosto, c.demoraCosto FROM Producto AS p JOIN Costo AS c ON p.idProducto = c.Producto_idProducto where idProducto='" + idProducto + "' ");
            demanda = (float)int.Parse(DS.Tables[0].Rows[0][0].ToString());
            sucursal = int.Parse(DS.Tables[0].Rows[0][1].ToString());
            idCosto = int.Parse(DS.Tables[0].Rows[0][2].ToString());
            costoB = float.Parse(DS.Tables[0].Rows[0][4].ToString());
            p = float.Parse(DS.Tables[0].Rows[0][5].ToString());
            costoA = int.Parse(DS.Tables[0].Rows[0][6].ToString());
            textBox21.Text = DS.Tables[0].Rows[0][7].ToString();
            textBox12.Text =textBox2.Text = "" + demanda;
            textBox3.Text = "" + sucursal;
            textBox4.Text = idCosto.ToString();
            textBox17.Text = textBox5.Text = costoB.ToString();
            textBox15.Text = textBox8.Text = costoA.ToString();
            textBox19.Text = "" + p;
            ECQ();
            T = Q / demanda;
            F=1/T;
            textBox9.Text = "" + T;
            textBox10.Text = "" + F;
            ECQFALTANTE();
            float dias = demanda/365;
           rop=dias*int.Parse(textBox21.Text);
            
            textBox21.Text = rop.ToString();
           

          
        
        
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            cInventario.Insert(textBox6, textBox7, textBox2, textBox8, textBox5, textBox9, textBox10,textBox21, "ECQ",idProducto, sucursal);
            updateGridView();
        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            cInventario.Delete(idInventario);
            updateId(idInventario);
            updateGridView();
        }

        private void getdatos()
        {
            DataSet DS;
            DS = cInventario.Consultation2("select *from Inventario where idInventario='" + idInventario + "'");
            textBox1.Text = DS.Tables[0].Rows[0][7].ToString();
            textBox2.Text = DS.Tables[0].Rows[0][2].ToString();
            textBox3.Text = DS.Tables[0].Rows[0][4].ToString();
            textBox4.Text = DS.Tables[0].Rows[0][5].ToString();
            textBox5.Text = DS.Tables[0].Rows[0][4].ToString();
            textBox6.Text = DS.Tables[0].Rows[0][0].ToString();
            textBox7.Text = DS.Tables[0].Rows[0][1].ToString();
            textBox8.Text = DS.Tables[0].Rows[0][3].ToString();
           

        }

        private void clean()
        {
            button1.Enabled = true;
            button3.Enabled = false;
            button4.Enabled = false;

            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
          
        
        }

        private void button2_Click(object sender, EventArgs e)
        {
            clean();
        }

        private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            button1.Enabled = false;
            button3.Enabled = true;
            button4.Enabled = true;
            idInventario = int.Parse(dataGridView1[0, e.RowIndex].Value.ToString());
            getdatos();
        }

        private void button5_Click_1(object sender, EventArgs e)
        {
            cInventario.Insert(textBox6, textBox7, textBox2, textBox8, textBox5, textBox9, textBox10, textBox21, "ECQFALTANTE", idProducto, sucursal);
            updateGridView();
        }

        private void textBox20_TextChanged(object sender, EventArgs e)
        {
            string filtro = textBox20.Text;
            dataGridView2.DataSource = cInventario.Consultation("SELECT * FROM Producto WHERE nombreProducto LIKE '%"+filtro+"%' ");
           
        }

        private void button4_Click(object sender, EventArgs e)
        {

        }
    }
}
